{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {},
   "outputs": [],
   "source": [
    "from ete3 import Tree\n",
    "t = Tree( \"((a,b),c);\" )\n",
    "t.show()\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [],
   "source": [
    "from ete3 import Tree\n",
    "t = Tree( \"((a,b),c);\" )\n",
    "t.render(\"mytree.png\", w=183, units=\"mm\")\n",
    "t.show()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 20,
   "metadata": {},
   "outputs": [],
   "source": [
    "from ete3 import Tree, TreeStyle\n",
    "t = Tree()\n",
    "t.populate(10, random_branches =True)\n",
    "ts = TreeStyle()\n",
    "ts.show_leaf_name = True\n",
    "ts.show_branch_length = True\n",
    "ts.show_branch_support = True\n",
    "t.show(tree_style=ts)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 19,
   "metadata": {},
   "outputs": [],
   "source": [
    "from ete3 import Tree, TreeStyle\n",
    "t = Tree()\n",
    "t.populate(10, random_branches =True)\n",
    "ts = TreeStyle()\n",
    "ts.show_leaf_name = True\n",
    "ts.scale = 120\n",
    "t.show(tree_style=ts)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 21,
   "metadata": {},
   "outputs": [],
   "source": [
    "from ete3 import Tree, TreeStyle\n",
    "t = Tree()\n",
    "t.populate(10, random_branches =True)\n",
    "ts = TreeStyle()\n",
    "ts.show_leaf_name = True\n",
    "ts.branch_vertical_margin = 10\n",
    "t.show(tree_style=ts)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 22,
   "metadata": {},
   "outputs": [],
   "source": [
    "from ete3 import Tree, TreeStyle\n",
    "t = Tree()\n",
    "t.populate(10, random_branches =True)\n",
    "ts = TreeStyle()\n",
    "ts.show_leaf_name = True\n",
    "ts.rotation = 90\n",
    "t.show(tree_style=ts)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 23,
   "metadata": {},
   "outputs": [],
   "source": [
    "from ete3 import Tree, TreeStyle\n",
    "t = Tree()\n",
    "t.populate(30)\n",
    "ts = TreeStyle()\n",
    "ts.show_leaf_name = True\n",
    "ts.mode = \"c\"\n",
    "ts.arc_start = -100\n",
    "ts.arc_span = 100\n",
    "t.show(tree_style=ts)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 31,
   "metadata": {},
   "outputs": [],
   "source": [
    "from ete3 import Tree, TreeStyle, TextFace\n",
    "t= Tree( \"((a,b),c);\" )\n",
    "ts = TreeStyle()\n",
    "ts.show_leaf_name = True\n",
    "ts.title.add_face(TextFace(\"ETE Tree\", fsize=20), column=0)\n",
    "t.show(tree_style=ts)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [],
   "source": [
    "from ete3 import Tree, faces, AttrFace, TreeStyle, NodeStyle\n",
    "\n",
    "def layout(node):\n",
    "    if node.is_leaf():\n",
    "        N = AttrFace(\"name\", fsize=30)\n",
    "        faces.add_face_to_node(N, node, 0, position=\"aligned\")\n",
    "\n",
    "def get_example_tree():\n",
    "\n",
    "    # Set dashed blue lines in all leaves\n",
    "    nst1 = NodeStyle()\n",
    "    nst1[\"bgcolor\"] = \"LightSteelBlue\"\n",
    "    nst2 = NodeStyle()\n",
    "    nst2[\"bgcolor\"] = \"Moccasin\"\n",
    "    nst3 = NodeStyle()\n",
    "    nst3[\"bgcolor\"] = \"DarkSeaGreen\"\n",
    "    nst4 = NodeStyle()\n",
    "    nst4[\"bgcolor\"] = \"Khaki\"\n",
    "\n",
    "\n",
    "    t = Tree(\"((((A,B),C), ((D,E),(F,G))), ((H,I),K));\")\n",
    "    for n in t.traverse():\n",
    "        n.dist = 0\n",
    "\n",
    "    n1 = t.get_common_ancestor(\"A\", \"B\", \"C\")\n",
    "    n1.set_style(nst1)\n",
    "    n2 = t.get_common_ancestor(\"D\", \"E\", \"F\", \"G\")\n",
    "    n2.set_style(nst2)\n",
    "    n3 = t.get_common_ancestor(\"H\", \"I\", \"K\")\n",
    "    n3.set_style(nst3)\n",
    "    n4 = t.get_common_ancestor(\"F\", \"G\")\n",
    "    n4.set_style(nst4)\n",
    "    ts = TreeStyle()\n",
    "    ts.layout_fn = layout\n",
    "    ts.show_leaf_name = False\n",
    "\n",
    "    ts.mode = \"c\"\n",
    "    ts.root_opening_factor = 1\n",
    "    return t, ts\n",
    "\n",
    "if __name__ == \"__main__\":\n",
    "    t, ts = get_example_tree()\n",
    "    #t.render(\"node_background.png\", w=400, tree_style=ts)\n",
    "    t.show(tree_style=ts)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [],
   "source": [
    "from ete3 import Tree, TreeStyle, TextFace\n",
    "t = Tree('Downloads/my_tree.fasta.final_tree.nw')\n",
    "ts = TreeStyle()\n",
    "ts.show_leaf_name = True\n",
    "ts.show_branch_length = True\n",
    "ts.show_branch_support = True\n",
    "ts.title.add_face(TextFace(\"my-tree\", fsize=40), column=0)\n",
    "t.show(tree_style=ts)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [],
   "source": [
    "from ete3 import Tree, TreeStyle, TextFace\n",
    "\n",
    "t = Tree('Downloads/my_tree.fasta.final_tree.nw')\n",
    "#t.populate(50)\n",
    "ts = TreeStyle()\n",
    "ts.mode = \"c\"\n",
    "ts.show_leaf_name = True\n",
    "ts.arc_start = -360 # 0 degrees = 3 o'clock\n",
    "ts.arc_span = 360\n",
    "t.show(tree_style=ts)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "228\n"
     ]
    }
   ],
   "source": [
    "from ete3 import Tree\n",
    "#t=Tree( \"((HAA:0.3,IAA:0.1):0.5,A:1,(BAA:0.4,(CAA:0.5,(JAA:1.3,(FAA:1.2,DAA:0.1):0.5):0.5):0.5):0.5);\" )\n",
    "t = Tree('Downloads/Project_master_thesis/output_tree/clustalo_default-trimal01-none-raxml_default_bootstrap/All_HA_march2020_cls95.fasta.final_tree.nw')\n",
    "#print (t)\n",
    "h=6.7 # the height value at which the tree will be trimmed \n",
    "\n",
    "first_node = t.get_tree_root()\n",
    "\n",
    "all_final_leaf=[]\n",
    "for nod in t.traverse('preorder'):\n",
    "    if len(nod.name) > 0:\n",
    "        all_final_leaf.append(nod.name)\n",
    "\n",
    "\n",
    "key_nodes=[]\n",
    "\n",
    "for nod in t.traverse('preorder'):\n",
    "    distance=t.get_distance(nod, first_node)\n",
    "    if distance > h and not True in [True for x in nod.get_ancestors() if [True for wazne in key_nodes if x ==wazne]]:\n",
    "        key_nodes.append(nod)\n",
    "        \n",
    "key_nodes_names=[]\n",
    "for nod in key_nodes:\n",
    "    if nod.is_leaf():\n",
    "        key_nodes_names.append([str(nod.name)])\n",
    "    else:\n",
    "        #key_nodes_names.append(\",\".join([str(x.name) for x in nod.get_leaves()]))\n",
    "        key_nodes_names.append([str(x.name) for x in nod.get_leaves()])\n",
    "        \n",
    "        \n",
    "flatten_list=[y for x in key_nodes_names for y in x if ',' not in y]\n",
    "\n",
    "for leaf in all_final_leaf:\n",
    "    if leaf not in flatten_list:\n",
    "        key_nodes_names.append([leaf])\n",
    "\n",
    "print(len(key_nodes_names))\n",
    "with open('some_silly_name_67_2.csv', 'w') as f:\n",
    "    for element in key_nodes_names:\n",
    "        f.write(\",\".join(map(str, element))+\"\\n\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'C:\\\\Users\\\\abo_a'"
      ]
     },
     "execution_count": 6,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "%pwd"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [],
   "source": [
    "ala='>gb:CY066943|ncbiId:ADK33681.1|UniProtKB:D8KV33|Organism:Influenza A virus (A/Aarhus/INS237/2009(H1N1))|Strain '"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      ">gb:CY066943|ncbiId:ADK33681.1|UniProtKB:D8KV33|Organism:Influenza A virus (A/Aarhus/INS237/2009(H1N1))|Strain \n"
     ]
    }
   ],
   "source": [
    "print(ala)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'H1N1'"
      ]
     },
     "execution_count": 9,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "ala.split('(')[2].split(')')[0]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.4"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
